/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker;

import com.nanosn.coffeemaker.java.ProjectClassLoader;
import com.nanosn.coffeemaker.project.ProjectSettings;
import com.nanosn.coffeemaker.project.ProjectSettingsFactory;
import com.nanosn.util.LocateFiles;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.groovy.control.CompilationFailedException;

/**
 * this class holds the constant global variables and the main
 * application functions.
 * @author selkhateeb
 */
public class Project {

    // <editor-fold defaultstate="collapsed" desc="Project Settings">
    // <editor-fold defaultstate="collapsed" desc="     Public Static Fields">
    /**
     * Gets the directory where the generated files are stored
     */
    public static String GENERATED_FILES_DIRECTORY = "generated";
    /**
     * Gets the package directory in which the template files are located.
     */
    public static String TEMPLATE_FILES_PACKAGE_DIRECTORY =
            "com/nanosn/coffeemaker/templates";
    /**
     * Gets the current directory
     */
    public static String PWD;

    static {
        // For performance and optimization
        PWD = System.getProperty("user.dir") + File.separator;
    }
    public static String NAME = "CoffeeMaker";
    public static String TMP;

    static {
        // For performance and optimization
        TMP = System.getProperty("java.io.tmpdir")
              + File.separator + Project.NAME
              + File.separator;
    }
    /**
     * Gets the global logger to be used across the application
     */
    public static Logger logger = Logger.global;

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="     Public Static Functions">
    /**
     * Pre: The application jar file exists.
     * Post: The full path of the application jar is returned as a File
     * @return a File object pointing at the application jar.
     * @throws java.io.IOException
     */
    public static File getApplicationJar() throws IOException {
        return LocateFiles.getClassLocation(new Project().getClass());
    }

    public static String getDevelopmentDirectory() {
        return Project.PWD + "development" + File.separator;
    }

    public static String getTestingDirectory() {
        return Project.PWD + "testing" + File.separator;
    }

    public static String getProductionDirectory() {
        return Project.PWD + "production" + File.separator;
    }

    /**
     * Gets the version information form the manifest file.
     * User passed Command-Line Argument: (-v --version)
     * Pre:
     * Post: The version information has returned
     * @return the version information as String
     * @throws java.io.IOException
     */
    public static String getVersion() throws IOException {

        Project app = new Project();
        Package pkg = app.getClass().getPackage();

        String version = pkg.getSpecificationTitle() + " - "
                         + pkg.getImplementationTitle() + " ("
                         + pkg.getSpecificationVersion()
                         + pkg.getImplementationVersion() + ")";

        return version;

    }
    // </editor-fold>
    // </editor-fold>

    public static ProjectClassLoader getClassLoader() throws Exception {
        File file = new File(Project.getDevelopmentDirectory());

        return new ProjectClassLoader(Project.class.getClassLoader(), file.toURI().toURL());
    }

//    public static String getProjectParentDirectory() throws Exception {
//        String pwd = Project.PWD.replace(getSettings().getRootPackage() + File.separator, "");
//
//        return pwd;
//    }
    public static ProjectSettings getSettings() {

        ProjectSettings ps = null;
        try {
            ps = ProjectSettingsFactory.getInstance();
        }
        catch (CompilationFailedException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ps;
    }
}

